/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import model.Filme;
import IRepositorios.IRepositorioFilme;
import control.Locadora;
import control.dao.metodosFilme;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Andre
 */
public class FormFilme extends javax.swing.JFrame {

    static int decisao;
    DefaultListModel<String> tmAtor = new DefaultListModel<>();
    private Locadora loc;
    private IRepositorioFilme IRFilme;
    DefaultTableModel tmFilme = new DefaultTableModel(null, new String[]{"Título", "Diretor", "Data Lançamento", "Preço", "Num. Cópias"});
    private List<Filme> listFilme;
    ListSelectionModel lsmFilme;
    static int codigoFilme;

    /**
     * Creates new form FormFilme
     */
    public FormFilme() {
        initComponents();
        BufferedImage image = null;
        File imageFile = new File(".\\src\\Imagens\\logo.png");
        try {
            image = ImageIO.read(imageFile);

        } catch (IOException ex) {
        }
        this.setIconImage(image);
        IRFilme = new metodosFilme();
        loc = new Locadora(IRFilme, null, null, null, null);
        listFilme = loc.buscarAllFilmeList();

        String[] generos = loc.buscarAllGeneros();
        if (generos!=null) {
            jCB_genero.setModel(new javax.swing.DefaultComboBoxModel(generos));
        }
        mostraPesquisa(listFilme);
        DesabilitarDados();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jTF_nome_genero = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jDialog2 = new javax.swing.JDialog();
        jTF_nome_ator = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jB_add_produtor = new javax.swing.JButton();
        jCB_ator = new javax.swing.JComboBox();
        jB_criarNewAtor = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jB_salvar3 = new javax.swing.JButton();
        jB_excluir = new javax.swing.JButton();
        jB_novo = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTF_sinopse = new javax.swing.JTextField();
        jTF_titulo = new javax.swing.JTextField();
        jTF_diretor = new javax.swing.JTextField();
        jTF_preco_locacao = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jFTF_datalancamento = new javax.swing.JFormattedTextField();
        jTF_num_copias = new javax.swing.JTextField();
        jTF_pesquisar = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jB_voltar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTabela_Filme = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList_atores = new javax.swing.JList();
        jLabel19 = new javax.swing.JLabel();
        jB_add_genero = new javax.swing.JButton();
        jB_add_ator = new javax.swing.JButton();
        jB_excluir_ator = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jCB_genero = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        jDialog1.setTitle("Adicionar elemento à lista");

        jTF_nome_genero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTF_nome_generoActionPerformed(evt);
            }
        });
        jTF_nome_genero.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTF_nome_generoKeyReleased(evt);
            }
        });

        jLabel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Nome"));

        jButton1.setText("Criar novo gênero");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jTF_nome_genero, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGap(210, 210, 210)
                        .addComponent(jButton1)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jTF_nome_genero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(199, Short.MAX_VALUE))
        );

        jTF_nome_ator.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTF_nome_atorKeyReleased(evt);
            }
        });

        jLabel7.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Nome"));

        jB_add_produtor.setText("Adicionar");
        jB_add_produtor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_add_produtorActionPerformed(evt);
            }
        });

        jCB_ator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione um ator" }));

        jB_criarNewAtor.setText("Criar novo ator");
        jB_criarNewAtor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_criarNewAtorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog2Layout.createSequentialGroup()
                .addGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jCB_ator, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jB_add_produtor, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jDialog2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jDialog2Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jDialog2Layout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addComponent(jTF_nome_ator, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(10, 10, 10))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog2Layout.createSequentialGroup()
                .addComponent(jB_criarNewAtor)
                .addContainerGap())
        );
        jDialog2Layout.setVerticalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog2Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jB_add_produtor, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCB_ator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jDialog2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jTF_nome_ator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jB_criarNewAtor, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(92, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Filmes");
        getContentPane().setLayout(null);

        jB_salvar3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jB_salvar3.setText("Salvar");
        jB_salvar3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_salvar3ActionPerformed(evt);
            }
        });
        getContentPane().add(jB_salvar3);
        jB_salvar3.setBounds(160, 550, 110, 40);

        jB_excluir.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jB_excluir.setText("Excluir");
        jB_excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_excluirActionPerformed(evt);
            }
        });
        getContentPane().add(jB_excluir);
        jB_excluir.setBounds(300, 550, 110, 40);

        jB_novo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jB_novo.setText("Novo");
        jB_novo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_novoActionPerformed(evt);
            }
        });
        getContentPane().add(jB_novo);
        jB_novo.setBounds(20, 550, 110, 40);

        jLabel3.setText("Diretor:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(310, 220, 50, 20);

        jLabel6.setText("Sinopse:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(30, 300, 50, 20);
        getContentPane().add(jTF_sinopse);
        jTF_sinopse.setBounds(80, 300, 460, 20);
        getContentPane().add(jTF_titulo);
        jTF_titulo.setBounds(70, 220, 220, 20);
        getContentPane().add(jTF_diretor);
        jTF_diretor.setBounds(350, 220, 150, 20);
        getContentPane().add(jTF_preco_locacao);
        jTF_preco_locacao.setBounds(320, 260, 60, 20);

        jLabel4.setText("Preço da locação:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(230, 260, 90, 20);

        jLabel5.setText("Num. de cópias:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(390, 260, 80, 20);

        jLabel9.setText("Titulo:");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(34, 220, 40, 20);

        try {
            jFTF_datalancamento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFTF_datalancamento.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(jFTF_datalancamento);
        jFTF_datalancamento.setBounds(140, 260, 80, 20);
        getContentPane().add(jTF_num_copias);
        jTF_num_copias.setBounds(470, 260, 60, 20);

        jTF_pesquisar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTF_pesquisarKeyReleased(evt);
            }
        });
        getContentPane().add(jTF_pesquisar);
        jTF_pesquisar.setBounds(180, 20, 230, 20);

        jLabel16.setText("Pesquisar:");
        getContentPane().add(jLabel16);
        jLabel16.setBounds(120, 20, 60, 20);

        jB_voltar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jB_voltar.setText("Voltar");
        jB_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_voltarActionPerformed(evt);
            }
        });
        getContentPane().add(jB_voltar);
        jB_voltar.setBounds(440, 550, 110, 40);

        jTabela_Filme.setModel(tmFilme);
        jTabela_Filme.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmFilme = jTabela_Filme.getSelectionModel();
        lsmFilme.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                if(! e.getValueIsAdjusting()){
                    JTTabelaLinhaSelecionada(jTabela_Filme);
                }
            }
        }
    );
    jScrollPane2.setViewportView(jTabela_Filme);

    getContentPane().add(jScrollPane2);
    jScrollPane2.setBounds(10, 50, 550, 120);

    jList_atores.setBackground(new java.awt.Color(240, 240, 240));
    jList_atores.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Atores", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.black));
    jList_atores.setModel(tmAtor);
    jList_atores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jList_atores.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    jScrollPane1.setViewportView(jList_atores);

    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(370, 340, 150, 120);

    jLabel19.setText("Data de lançamento :");
    getContentPane().add(jLabel19);
    jLabel19.setBounds(30, 260, 110, 20);

    jB_add_genero.setText("Adicionar novo gênero");
    jB_add_genero.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jB_add_generoActionPerformed(evt);
        }
    });
    getContentPane().add(jB_add_genero);
    jB_add_genero.setBounds(90, 380, 150, 23);

    jB_add_ator.setText("Add Ator");
    jB_add_ator.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jB_add_atorActionPerformed(evt);
        }
    });
    getContentPane().add(jB_add_ator);
    jB_add_ator.setBounds(390, 470, 110, 23);

    jB_excluir_ator.setText("Excluir Ator");
    jB_excluir_ator.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jB_excluir_atorActionPerformed(evt);
        }
    });
    getContentPane().add(jB_excluir_ator);
    jB_excluir_ator.setBounds(390, 500, 110, 23);

    jLabel8.setText("Gênero:");
    getContentPane().add(jLabel8);
    jLabel8.setBounds(30, 350, 40, 14);

    jCB_genero.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "selecione um gênero" }));
    getContentPane().add(jCB_genero);
    jCB_genero.setBounds(80, 345, 170, 20);

    jLabel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Dados Gerais"));
    getContentPane().add(jLabel2);
    jLabel2.setBounds(10, 190, 550, 350);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-583)/2, (screenSize.height-643)/2, 583, 643);
    }// </editor-fold>//GEN-END:initComponents

    private void jB_salvar3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_salvar3ActionPerformed
        metodosFilme mf = new metodosFilme();
        
        if (VerificaDados()) {
            Filme filme = null;
            try {
                filme = atribuiValores();
            } catch (ParseException ex) {
                Logger.getLogger(FormFilme.class.getName()).log(Level.SEVERE, null, ex);
            }


            if (decisao == 0) {
                if (loc.buscarFilme(filme.getTitulo()) == null) {
                    //cadastrar
                    loc.cadastrarFilme(filme);
                    limpaDados();
                    DesabilitarDados();
                    jTF_pesquisarKeyReleased(null);
                    JOptionPane.showMessageDialog(null, "Cadastrado com sucesso.");
                } else {
                    JOptionPane.showMessageDialog(null, "Filme ja cadastrado.");
                }

            } else {
                //editar
                loc.atualizarCadastro(filme);
                limpaDados();

                DesabilitarDados();
                jTF_pesquisarKeyReleased(null);
                JOptionPane.showMessageDialog(null, "Editado com sucesso.");
            }

        } else {
            JOptionPane.showMessageDialog(null, "Campos não preenchidos.");
        }
    }//GEN-LAST:event_jB_salvar3ActionPerformed

    private void jB_excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_excluirActionPerformed
        if (jTabela_Filme.getSelectedRow() != -1) {
            loc.removerFilme(listFilme.get(jTabela_Filme.getSelectedRow()).getCodigo());

            limpaDados();
            DesabilitarDados();
            jTF_pesquisarKeyReleased(null);
        }


    }//GEN-LAST:event_jB_excluirActionPerformed

    private void jB_novoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_novoActionPerformed
        jTabela_Filme.clearSelection();
        decisao = 0;
        limpaDados();
        AbilitarDados();
    }//GEN-LAST:event_jB_novoActionPerformed

    private void jTF_pesquisarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_pesquisarKeyReleased
        listFilme = loc.buscarFilmeList(jTF_pesquisar.getText() + "%");
        mostraPesquisa(listFilme);
    }//GEN-LAST:event_jTF_pesquisarKeyReleased

    private void jB_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_voltarActionPerformed
        dispose();
    }//GEN-LAST:event_jB_voltarActionPerformed

    private void jB_add_generoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_add_generoActionPerformed

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        jDialog1.setBounds((screenSize.width - 375) / 2, (screenSize.height - 145) / 2, 375, 145);
        jDialog1.setVisible(true);
    }//GEN-LAST:event_jB_add_generoActionPerformed

    private void jB_excluir_atorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_excluir_atorActionPerformed
        if (jList_atores.getSelectedIndex() > -1) {
            tmAtor.remove(jList_atores.getSelectedIndex());
        } else {
            JOptionPane.showMessageDialog(null, "Nada Selecionado.");
        }
    }//GEN-LAST:event_jB_excluir_atorActionPerformed

    private void jB_add_atorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_add_atorActionPerformed

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        jDialog2.setBounds((screenSize.width - 410) / 2, (screenSize.height - 250) / 2, 410, 250);
        jDialog2.setVisible(true);
        String[] atores = loc.buscarAllAtores();
        if (atores!=null) {
            jCB_ator.setModel(new javax.swing.DefaultComboBoxModel(atores));
        }
    }//GEN-LAST:event_jB_add_atorActionPerformed

    private void jTF_nome_generoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_nome_generoKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {


            tmAtor.addElement(jTF_nome_ator.getText());


            jTF_nome_ator.setText("");
            JOptionPane.showMessageDialog(null, "Adicionado");
        }
    }//GEN-LAST:event_jTF_nome_generoKeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        loc.criarGenero(jTF_nome_genero.getText());
        String[] generos = loc.buscarAllGeneros();
        if (generos!=null) {
            jCB_genero.setModel(new javax.swing.DefaultComboBoxModel(generos));
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTF_nome_atorKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTF_nome_atorKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jTF_nome_atorKeyReleased

    private void jB_add_produtorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_add_produtorActionPerformed
        if(!String.valueOf(jCB_ator.getSelectedItem()).equals("Selecione um ator")){
        tmAtor.addElement((String) jCB_ator.getSelectedItem());
        JOptionPane.showMessageDialog(null, "Adicionado");
        }
    }//GEN-LAST:event_jB_add_produtorActionPerformed

    private void jB_criarNewAtorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_criarNewAtorActionPerformed
        loc.criarAtor(jTF_nome_ator.getText());
        String[] atores = loc.buscarAllAtores();
        if (atores!=null) {
            jCB_ator.setModel(new javax.swing.DefaultComboBoxModel(atores));
        }
    }//GEN-LAST:event_jB_criarNewAtorActionPerformed

    private void jTF_nome_generoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTF_nome_generoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTF_nome_generoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FormFilme().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_add_ator;
    private javax.swing.JButton jB_add_genero;
    private javax.swing.JButton jB_add_produtor;
    private javax.swing.JButton jB_criarNewAtor;
    private javax.swing.JButton jB_excluir;
    private javax.swing.JButton jB_excluir_ator;
    private javax.swing.JButton jB_novo;
    private javax.swing.JButton jB_salvar3;
    private javax.swing.JButton jB_voltar;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCB_ator;
    private javax.swing.JComboBox jCB_genero;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JFormattedTextField jFTF_datalancamento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList_atores;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTF_diretor;
    private javax.swing.JTextField jTF_nome_ator;
    private javax.swing.JTextField jTF_nome_genero;
    private javax.swing.JTextField jTF_num_copias;
    private javax.swing.JTextField jTF_pesquisar;
    private javax.swing.JTextField jTF_preco_locacao;
    private javax.swing.JTextField jTF_sinopse;
    private javax.swing.JTextField jTF_titulo;
    private javax.swing.JTable jTabela_Filme;
    // End of variables declaration//GEN-END:variables

    private void limpaDados() {
        jTF_diretor.setText("");
        jTF_num_copias.setText("");
        jTF_preco_locacao.setText("");
        jTF_titulo.setText("");
        jFTF_datalancamento.setText("");
        jList_atores.removeAll();

        jTF_sinopse.setText("");

        tmAtor.clear();

    }

    private void AbilitarDados() {
        jTF_diretor.setEditable(true);
        jTF_num_copias.setEditable(true);
        jTF_preco_locacao.setEditable(true);
        jTF_titulo.setEditable(true);
        jFTF_datalancamento.setEditable(true);
        jList_atores.setVisible(true);

        jB_add_ator.setVisible(true);
        jB_add_genero.setVisible(true);
        jB_excluir_ator.setVisible(true);
        jCB_genero.setVisible(true);
        jB_add_genero.setVisible(true);
        jLabel8.setVisible(true);

        jScrollPane1.setVisible(true);

        jTF_sinopse.setEditable(true);

    }

    private void DesabilitarDados() {
        jTF_diretor.setEditable(false);
        jTF_num_copias.setEditable(false);
        jTF_preco_locacao.setEditable(false);
        jTF_titulo.setEditable(false);
        jFTF_datalancamento.setEditable(false);
        jList_atores.setVisible(false);

        jB_add_ator.setVisible(false);
        jB_add_genero.setVisible(false);
        jB_excluir_ator.setVisible(false);
        jCB_genero.setVisible(false);
        jB_add_genero.setVisible(false);
        jLabel8.setVisible(false);
        jScrollPane1.setVisible(false);

        jTF_sinopse.setEditable(false);
    }

    private void mostraPesquisa(List<Filme> listFilme) {
        while (tmFilme.getRowCount() > 0) {
            tmFilme.removeRow(0);
        }
        if (!listFilme.isEmpty()) {
            String[] linha = new String[]{null, null, null, null, null};
            for (int i = 0; i < listFilme.size(); i++) {
                tmFilme.addRow(linha);
                tmFilme.setValueAt(listFilme.get(i).getTitulo(), i, 0);
                tmFilme.setValueAt(listFilme.get(i).getDiretor(), i, 1);
                tmFilme.setValueAt(listFilme.get(i).getDataLancamento(), i, 2);
                tmFilme.setValueAt(listFilme.get(i).getPrecoLocacao(), i, 3);
                tmFilme.setValueAt(listFilme.get(i).getNumeroCopias(), i, 4);


            }
        }

    }

    private void JTTabelaLinhaSelecionada(JTable tabela) {
        if (jTabela_Filme.getSelectedRow() > -1) {
            AbilitarDados();
            decisao = 1;
            jTF_titulo.setText(String.valueOf(listFilme.get(tabela.getSelectedRow()).getTitulo()));
            jTF_diretor.setText(String.valueOf(listFilme.get(tabela.getSelectedRow()).getDiretor()));
            jTF_num_copias.setText(String.valueOf(listFilme.get(tabela.getSelectedRow()).getNumeroCopias()));
            jTF_preco_locacao.setText(String.valueOf(listFilme.get(tabela.getSelectedRow()).getPrecoLocacao()));
            jFTF_datalancamento.setText(new SimpleDateFormat("dd/MM/yyyy").format(listFilme.get(tabela.getSelectedRow()).getDataLancamento()));
            jTF_sinopse.setText(String.valueOf(listFilme.get(tabela.getSelectedRow()).getSinopse()));
            jCB_genero.setSelectedItem(listFilme.get(tabela.getSelectedRow()).getGenero());
            codigoFilme = listFilme.get(tabela.getSelectedRow()).getCodigo();

            tmAtor.clear();
            for (int i = 0; listFilme.get(tabela.getSelectedRow()).getAtores().size() > i; i++) {

                if (!listFilme.get(tabela.getSelectedRow()).getAtores().isEmpty()) {

                    tmAtor.add(i, listFilme.get(tabela.getSelectedRow()).getAtores().get(i));
                }

            }




        } else {
            limpaDados();
            DesabilitarDados();

        }
    }

    private boolean VerificaDados() {
        if ("".equals(jTF_diretor.getText()) || "".equals(jTF_num_copias.getText()) || "".equals(jTF_preco_locacao.getText()) || "".equals(jTF_sinopse.getText()) || "".equals(jTF_titulo.getText())) {
            return false;
        } else {
            return true;
        }
    }

    private Filme atribuiValores() throws ParseException {
        Filme filme = new Filme();
        filme.setCodigo(codigoFilme);
        filme.setTitulo(jTF_titulo.getText());
        filme.setDiretor(jTF_diretor.getText());
        filme.setNumeroCopias(Integer.parseInt(jTF_num_copias.getText()));
        filme.setPrecoLocacao(Float.parseFloat(jTF_preco_locacao.getText()));
        filme.setSinopse(jTF_sinopse.getText());
        filme.setDataLancamento(formatarData(jFTF_datalancamento.getText()));
        filme.setGenero((String) jCB_genero.getSelectedItem());
        List<String> ator = new ArrayList<>();


        for (int i = 0; tmAtor.getSize() > i; i++) {
            ator.add(tmAtor.get(i));
        }



        filme.setAtores(ator);

        return filme;
    }

    private Date formatarData(String data) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        return new Date(formatter.parse(data).getTime());

    }
}